//
//  HisoryViewController.m
//  groupProject
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "HisoryViewController.h"
#import "Config.h"
@interface HisoryViewController ()

@end

@implementation HisoryViewController{
    NSMutableArray * dataArray1;  // 临时数据1
    NSMutableArray * dataArray2; // 临时数据2
    UITableViewCellEditingStyle selectEditingStyle;
    UIBarButtonItem *EditBtn;
    UIBarButtonItem *CanceBtn;
    UIBarButtonItem * BackBtn;
    
    UIView * BaseView;
    UIButton * SelectBtn;
    BOOL inSelect;
}
@synthesize Hisorysegment,HisoryTableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.tabBarController.tabBar.hidden = YES;
    inSelect = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = [self addHeardBtn];
    HisoryTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 47)];
    HisoryTableView.delegate = self;
    HisoryTableView.dataSource = self;
    [self.view addSubview:HisoryTableView];
    //编辑右BTN
    EditBtn = [[UIBarButtonItem alloc]initWithTitle:@"编辑"style:UIBarButtonItemStylePlain target:self action:@selector(HisoryEdit)];
    //点击编辑后的右BTN
    CanceBtn = [[UIBarButtonItem alloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlain target:self action:@selector(HisoryCance)];
    
    self.navigationItem.rightBarButtonItem = EditBtn;
    BackBtn =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(BackHome)];
    self.navigationItem.leftBarButtonItem = BackBtn;
    
    //临时数据
    dataArray1 = [NSMutableArray new];
    dataArray2 = [NSMutableArray new];
    for (unsigned int itemNumber = 1; itemNumber <= 12; itemNumber++)
    {
        NSString *itemName = [NSString stringWithFormat:@"iPhone %d", itemNumber];
        [dataArray1 addObject:itemName];
    }
    for (unsigned int itemNumber = 1; itemNumber <= 15; itemNumber++)
    {
        NSString *itemName = [NSString stringWithFormat:@"iPhone %d", itemNumber];
        [dataArray2 addObject:itemName];
    }
    
    
}
//坐标还需修改
-(UIView *)addHeardBtn{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.1-25, 0, SCREEN_WIDTH/2, 30)];
    NSArray * segmentArray = [NSArray arrayWithObjects:@"看过的",@"买过的", nil];
    Hisorysegment = [[UISegmentedControl alloc]initWithItems:segmentArray];
    Hisorysegment.frame = CGRectMake(SCREEN_WIDTH*0.1-25,0, SCREEN_WIDTH/2,30);
    Hisorysegment.selectedSegmentIndex =0;
    Hisorysegment.tintColor= [UIColor redColor];
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],
                                             NSForegroundColorAttributeName: [UIColor whiteColor]};
    [Hisorysegment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                               NSForegroundColorAttributeName: [UIColor redColor]};
    [Hisorysegment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    [Hisorysegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [view addSubview:Hisorysegment];
    return view;
    
}
-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger index = Seg.selectedSegmentIndex;
    NSLog(@"Index %li",(long)index);
    switch (index) {
        case 0:
            [HisoryTableView reloadData];
            [self HisoryCance];
            break;
        case 1:
            [HisoryTableView reloadData];
            [self HisoryCance];
            break;
        default:
            break;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (Hisorysegment.selectedSegmentIndex == 0) {
        return dataArray1.count;
    }else if(Hisorysegment.selectedSegmentIndex == 1)
    {
        return dataArray2.count;
    }
    return 0;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * tmpArray = [[NSMutableArray alloc]init];
    if (Hisorysegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Hisorysegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.text = [tmpArray objectAtIndex:indexPath.row];
    return cell;
}
-(void)BackHome{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
-(void)HisoryEdit{
    self.HisoryTableView.allowsMultipleSelectionDuringEditing = YES;  // 可进行多选
    
    [HisoryTableView setEditing:YES  animated:YES];
    selectEditingStyle =UITableViewCellEditingStyleDelete;
    [self updateBarButtons];
    [self addBaseBtnOfDeledate];
    
}
// 是否可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
//返回编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return selectEditingStyle;
}

-(void) updateBarButtons{
    if (HisoryTableView.allowsSelectionDuringEditing == YES){
        
        self.navigationItem.rightBarButtonItem = CanceBtn;
        return;
    }
    if (HisoryTableView.editing == NO) {
        self.navigationItem.rightBarButtonItem = EditBtn;
    }
}
//点击编辑时  底部显示编辑工具栏
-(void)addBaseBtnOfDeledate{
    BaseView = [[UIView alloc]initWithFrame:CGRectMake(0, 620, SCREEN_WIDTH, 47)];
    BaseView.layer.borderWidth =1;
    BaseView.layer.borderColor = [[UIColor lightGrayColor]CGColor ];
    BaseView.backgroundColor = [UIColor whiteColor];
    
    UIButton * deleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 5, 150, 35)];
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteBtn.backgroundColor = [UIColor redColor];
    
    [deleteBtn addTarget:self action:@selector(selectedDeleteClick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel * HisLable = [[UILabel alloc]initWithFrame:CGRectMake(35, 8, 50, 35)];
    HisLable.text = @"全选";
    
    SelectBtn = [[UIButton alloc]initWithFrame:CGRectMake(5, 10, 20, 20)];
    SelectBtn.layer.borderWidth =1;
    SelectBtn.layer.borderColor = [[UIColor lightGrayColor]CGColor ];
    [SelectBtn addTarget:self action:@selector(SelectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:BaseView];
    [BaseView addSubview:deleteBtn];
    [BaseView addSubview:HisLable];
    [BaseView addSubview:SelectBtn];
    
}
-(void)selectedDeleteClick:(NSMutableArray *)tmpArray {
    if (Hisorysegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Hisorysegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    // 选中的行
    NSArray * selectedRows = [HisoryTableView indexPathsForSelectedRows];
    // 是否删除特定的行
    BOOL deleteSpecificRows = selectedRows.count > 0;
    if (deleteSpecificRows)
    {
        // 将所选的行的索引值放在一个集合中进行批量删除
        NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
        
        for (NSIndexPath *selectionIndex in selectedRows)
        {
            [indicesOfItemsToDelete addIndex:selectionIndex.row];
        }
        // 从数据源中删除所选行对应的值
        [tmpArray removeObjectsAtIndexes:indicesOfItemsToDelete];
        
        //删除所选的行
        [HisoryTableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    
}


-(void)HisoryCance{
    [HisoryTableView setEditing:NO animated:YES];
    
    HisoryTableView.allowsMultipleSelectionDuringEditing = NO;
    [self updateBarButtons];
    BaseView.hidden = YES;
}
-(void)SelectBtnClick:( NSMutableArray *)tmpArray{
    
    if (Hisorysegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Hisorysegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    if (inSelect) {
        [SelectBtn setImage:[UIImage imageNamed:@"Hook.jpg"] forState:UIControlStateNormal];
        
        for (int i = 0; i < tmpArray.count; i++) {
            NSIndexPath * ipath = [NSIndexPath indexPathForRow:i inSection:0];
            [HisoryTableView selectRowAtIndexPath:ipath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
        inSelect = NO;
        
    } else
    {
        [SelectBtn setImage:nil forState:UIControlStateNormal];
        for (int i = 0; i < tmpArray.count; i++) {
            NSIndexPath * ipath = [NSIndexPath indexPathForRow:i inSection:0];
            [HisoryTableView deselectRowAtIndexPath:ipath animated:YES];
            
        }
        
        inSelect = YES;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray * tmpArray = [[NSMutableArray alloc]init];
    if (Hisorysegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Hisorysegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    if (tableView.editing == YES) {
        NSLog(@"didSelectRow");
        if (tableView.indexPathsForSelectedRows.count == tmpArray.count) {
            [SelectBtn setImage:[UIImage imageNamed:@"Hook.jpg"] forState:UIControlStateNormal];
            inSelect = NO;
        }else {
            [SelectBtn setImage:nil forState:UIControlStateNormal];
            inSelect = YES;
        }
    } else {
        NSLog(@"-------111");
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSMutableArray * tmpArray = [[NSMutableArray alloc]init];
    if (Hisorysegment.selectedSegmentIndex == 0) {
        tmpArray = dataArray1;
    }else if(Hisorysegment.selectedSegmentIndex == 1){
        tmpArray = dataArray2;
    }
    if (tableView.editing == YES) {
        NSLog(@"didDeselectRowAt");
        if (tableView.indexPathsForSelectedRows.count != tmpArray.count) {
            [SelectBtn setImage:nil forState:UIControlStateNormal];
            inSelect = YES;
        }
    } else {
        NSLog(@"-------1222");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

@end
